﻿// Jeremy Lee
// Battle Tactics- A turn based strategy game
// Menu.cs- The Unit selection menu, and a class representing the cursor
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace BattleTactics
{
    public class MenuItem
    {
        public string text;
        public bool selected;
        public bool disabled;

        public MenuItem()
        {
            text = "Empty Choice";
            selected = false;
            disabled = false;
        }
        public MenuItem(string choice)
        {
            text = choice;
            selected = false;
            disabled = false;
        }
        public void draw(SpriteBatch sb, Vector2 loc)
        {
            Color tint = Color.Black;
            if (selected)
                tint = Color.Red;
            if (disabled)
                tint = Color.DarkGray;
            sb.DrawString(Globl.font, text, loc, tint, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.9f);
        }
    }

    public class Menu
    {
        public int quadrant;
        float xMod;
        float yMod;
        public int choice;
        VectorInt size;
        public MenuItem[] menu;
        const float width = 75.0f;
        const float height = 60.0f;

        public Menu()
        {
            quadrant = 1;
            choice = 0;
            size = new VectorInt(100, 50);
            menu = new MenuItem[3];
            menu[0] = new MenuItem("Move");
            menu[0].selected = true;
            menu[1] = new MenuItem("Attack");
            menu[2] = new MenuItem("End");
        }
        public void changeChoice(int i)
        {
            int nextChoice = (choice + i) % 3;
            while (menu[nextChoice].disabled)
                nextChoice = (nextChoice + i) % 3;
            choice = nextChoice;
        }

        public void update()
        {
            quadrant = Globl.cursor.quadrant;
            if (quadrant == 1 || quadrant == 4)
                xMod = -1.0f * width;
            else
                xMod = (float)Globl.TILE_SIZE;
            if (quadrant == 1 || quadrant == 2)
                yMod = 0.0f;
            else
                yMod = -1.0f * height + (float)Globl.TILE_SIZE;
            for (int i = 0; i < 3; i++)
                menu[i].selected = false;
            if (menu[choice].disabled)
                changeChoice(1);
            menu[choice].selected = true;
        }
        public void draw(SpriteBatch sb)
        {
            float xLoc = (float)(Globl.cursor.loc.X * Globl.TILE_SIZE) + xMod;
            float yLoc = (float)(Globl.cursor.loc.Y * Globl.TILE_SIZE) + yMod;
            Vector2 loc = new Vector2(xLoc, yLoc);
            sb.Draw(Globl.blank, loc, new Rectangle(0, 0, 1, 1), Color.White, 
                    0.0f, Vector2.Zero, new Vector2(width, height), 
                    SpriteEffects.None, 0.8f);
            for (int i = 0; i < 3; i++)
                menu[i].draw(sb, loc + new Vector2(5.0f, 5.0f + 17.0f * (float)i));
        }
    }

    public class StatusBox
    {
        public int quadrant;
        public List<string> lines;
        Vector2 loc;

        public StatusBox()
        {
            loc = Vector2.Zero;
            quadrant = 1;
            lines = new List<string>();
        }
        public StatusBox(string[] line)
        {
            loc = Vector2.Zero;
            quadrant = 1;
            lines = new List<string>();
            lines.AddRange(line);
        }
        public void add( IEnumerable<string> line )
        {
            lines.AddRange( line );
        }
        public void removeAll()
        {
            if( lines.Count>0 )
                lines.RemoveRange(0, lines.Count);
        }
        public void update()
        {
            quadrant = (Globl.cursor.quadrant + 2) % 4;
            if (quadrant == 0)
                quadrant = 4;
            if (quadrant == 1 || quadrant == 4)
                loc.X = 700.0f;
            else
                loc.X = 0.0f;
            if (quadrant == 1 || quadrant == 2)
                loc.Y = 0.0f;
            else
                loc.Y = 450.0f;
        }

        public void draw(SpriteBatch sb)
        {
            sb.Draw(Globl.blank, loc, new Rectangle(0, 0, 1, 1), new Color( 1.0f, 1.0f, 1.0f, 0.7f ),
                    0.0f, Vector2.Zero, new Vector2(100.0f, 150.0f),
                    SpriteEffects.None, 0.75f);
            for (int i = 0; i < lines.Count; i++)
                sb.DrawString(Globl.font, lines[i], loc + new Vector2(5.0f, 5.0f + 17.0f * (float)i), Color.Black,
                              0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.9f);
        }
    }

    public class Cursor
    {
        public VectorInt loc;
        public int quadrant;
        public Texture2D sprite;

        public Cursor()
        {
            sprite = Globl.content.Load<Texture2D>("sprites\\cursor");
            loc = new VectorInt(7, 5);
        }
        public void update()
        {
            if (loc.X > Globl.TILES_WIDE / 2)
                if (loc.Y < Globl.TILES_TALL / 2)
                    quadrant = 1;
                else
                    quadrant = 4;
            else
                if (loc.Y < Globl.TILES_TALL / 2)
                    quadrant = 2;
                else
                    quadrant = 3;
        }

        public void draw(SpriteBatch sb)
        {
            sb.Draw(sprite, 
                    new Rectangle(loc.X * Globl.TILE_SIZE, 
                                  loc.Y * Globl.TILE_SIZE, 
                                  Globl.TILE_SIZE, 
                                  Globl.TILE_SIZE),
                    new Rectangle( 0, 0, sprite.Width, sprite.Height),
                    Color.White, 0.0f, Vector2.Zero, SpriteEffects.None,
                    0.8f );
        }
    }
    public class Help
    {
        public string[] lines;

        public Help()
        {
            lines = new string[] { "========================HELP=======================",
                                   "Use the DPad or Keyboard Arrows to move the cursor",
                                   "Press A on the Gamepad or Z on the keyboard to select",
                                   "a unit or confirm an option. Press B on the Gamepad",
                                   "or X on the keyboard to cancel.",
                                   "Music from Final Fantasy VI" ,
                                   "Sprites from Fire Emblem 4" };
        }

        public void draw(SpriteBatch sb)
        {
            sb.Draw(Globl.blank, new Vector2( 50.0f ), new Rectangle(0, 0, 1, 1), Color.White,
                    0.0f, Vector2.Zero, new Vector2(700.0f, 500.0f),
                    SpriteEffects.None, 0.99f);
            for (int i = 0; i < lines.Length; i++)
                sb.DrawString(Globl.font, lines[i], new Vector2(150.0f, 175.0f + 25.0f * (float)i), Color.Black,
                              0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
        }
    }
}
